package com.servlet;

import com.dao.UserDao;
import com.entity.User;
import com.util.SpringUtil;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;

@WebServlet("/admin/getImg")
public class GetImg extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username =(String) req.getSession().getAttribute("username");

        UserDao userDao = SpringUtil.getBean(UserDao.class);
        User user = userDao.SelectUserByUsername(username);
        String touxiang = user.getTouxiang();

        ServletOutputStream outputStream = resp.getOutputStream();
        System.out.println("D:\\upload\\"+touxiang);
        FileInputStream fileInputStream = new FileInputStream("D:\\upload\\"+touxiang);
        byte[] bytes=new byte[1024];
        int i = fileInputStream.read(bytes);
        while (i!=-1){
            outputStream.write(bytes,0,i);
            i = fileInputStream.read(bytes);
        }

        outputStream.close();


    }
}
